package org.destiny;

import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TestService {

    @Autowired
    Validator validator;

    public String testValidator(StudentDTO student) {
        String result = null;
        // 第二个参数其实是个可变参数，可以不传
        Set<ConstraintViolation<StudentDTO>> constraintViolations = validator.validate(student);

        for (ConstraintViolation<StudentDTO> constraintViolation : constraintViolations) {
            System.out.println("对象属性:" + constraintViolation.getPropertyPath());
            System.out.println("国际化key:" + constraintViolation.getMessageTemplate());
            System.out.println("错误信息:" + constraintViolation.getMessage());

            result = constraintViolation.getPropertyPath() + constraintViolation.getMessage();
        }
        return result;
    }
}
